/**
 * 分页查询列表
 */

import base from './list'
import $ from '@/common'
export default {
    name: 'page_list',
    extends: base,
    state: {
        pageOn: 1,
        pageSize: 20,
        pageCount: 0,
        totalCount: 0,
        fetchPage: 1,
        loading: false,
        path: null,
        fetchMethod: 'post',
        filterKey: false,
        filterObj: {},
        filterFixed: {},
        filterNow: null,
        // filter属性是否在request body中
        bodyFilter: false,
        // 额外查询参数
        extPost: null,
        // model是否已读取，用于处理需要同时处理model与data的情况
        modelLoad: false,
        enablePage: true
    },
    getters: {
        fetchPath(state) {
            return state.path || state.listModel.path
        },
        fetchParam(state) {
            var data = {}
            if (state.enablePage) {
                data = {
                    pageNo: state.fetchPage,
                    pageSize: state.pageSize
                }
            }
            if (!state.filterKey || !$.trim(state.filterKey).length) {
                $.setObj(data, state.filterObj)
            } else if (state.bodyFilter) {
                data.filter = state.filterKey
            }

            if (state.filterFixed) {
                $.setProp(data, state.filterFixed)
            }
            if (state.extPost) {
                $.setProp(data, state.extPost)
            }
            return data
        }
    },
    actions: {
        fetch({ dispatch, commit, state, getters }, page = 1) {
            commit('setFetchPage', page)
            commit('setLoading', true)
            var url = getters.fetchPath
            if (state.filterKey && !state.bodyFilter) {
                url = $.setUrlQuery(url, {
                    filter: state.filterKey
                })
            }
            return $.link(url, getters.fetchParam, {
                method: state.fetchMethod
            }).then((res) => {
                var data = res.data
                if (data.data && data.model) {
                    data = data.data
                }
                if (data) {
                    if (data.data) {
                        commit('setPageData', data)
                    } else {
                        commit('setData', data)
                    }
                }
                // 记录当前过滤数据
                if (state.filterObj) {
                    commit('setFilterNow', state.filterObj)
                }
            }).catch((ex) => {
                console.error(ex)
            }).finally(() => {
                commit('setLoading', false)
            })
        },
        pageSizeChange({ commit, dispatch, state }, size) {
            commit('setPageSize', size)
            dispatch('fetch')
            $.localPageSize(state.bill_code || state.listModel.bill_code || state.listModel.code, size)
        },
        filter({ commit, dispatch }, key) {
            if (key && (typeof key === 'object')) {
                commit('setFilterObj', key)
                commit('setFilterKey', null)
            } else {
                commit('setFilterObj', {})
                commit('setFilterKey', key)
            }
            dispatch('fetch')
        },
        update() {},
        importFilter({ commit }, data) {
            // commit('search/setData', $.copy(data))
            commit('setFilterObj', data)
            commit('setFilterKey', null)
        }
    },
    mutations: {
        setPageData(state, data) {
            $.set(state, 'listData', data.data || data)
            state.pageOn = data.pageNo || 1
            state.pageSize = data.pageSize
            state.pageCount = data.totalPageCount
            state.totalCount = data.totalCount
            state.loading = false
        },
        setFetchPage(state, v) {
            state.fetchPage = v
        },
        setPath(state, v) {
            state.path = v
        },
        setFilterKey(state, v) {
            state.filterKey = v
        },
        setFilterObj(state, v) {
            $.set(state, 'filterObj', v)
        },
        setLoading(state, v) {
            state.loading = v
        },
        setBodyFilter(state, v) {
            state.bodyFilter = v
        },
        setFetchMethod(state, v) {
            state.fetchMethod = v
        },
        setFixedFilter(state, v) {
            $.set(state, 'filterFixed', v)
        },
        setExtPost(state, v) {
            $.set(state, 'extPost', $.copy(v))
        },
        setPageSize(state, size) {
            state.pageSize = size
        },
        setFilterNow(state, v) {
            $.set(state, 'filterNow', $.copy(v))
        },
        clearStatus(state) {
            $.set(state, 'listData', [])
            $.setProp(state, {
                pageOn: 1,
                pageSize: state.pageSize,
                pageCount: 1,
                totalCount: 0,
                fetchPage: 1,
                loading: false,
                filterKey: false,
                filterObj: false
            })
        }
    }
}